//
//  CommitConsultVC.m
//  sensen_ios
//
//  Created by pro on 16/2/25.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "CommitConsultVC.h"

@interface CommitConsultVC ()

@end

@implementation CommitConsultVC

#pragma mark - 控制器视图方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"购买咨询";
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    [self createLeftBarButtonItemWithImagename:nil]; // 创建导航栏返回按钮
    
    [self _createViews]; // 创建视图
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
}

#pragma mark - 创建视图
- (void)_createViews {
    
    // 声明 视图
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = SCREEN_WIDTH;
    CGFloat height = 40;
    
    UIView *statementBgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    statementBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:statementBgView];

    x = 20;
    y = 10;
    width = statementBgView.width - 2 * x;
    UILabel *statementLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    statementLabel.font = FONT(10);
    statementLabel.numberOfLines = 0;
    statementLabel.textColor = [UIColor grayColor];
    statementLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:statementLabel];
    
    NSString *statementContent = @"声明：您可对产品包装、颜色、运输、库存等方面进行咨询,我们有专人进行回复!因厂家随时会更改一些产品的包装、颜色、产地等参数,所以该回复仅在当时对提问者有效,其他网友仅供参考!咨询回复的工作时间为：周一至周五,9:00至18:00,请耐心等待工作人员回复。";
    
    //一 创建NSMutableAttributedString 对象
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:statementContent];
    // 字体颜色
    [attrString addAttribute:NSForegroundColorAttributeName
                       value:SS_NAVI_ORANGE_COLOR
                       range:[statementContent rangeOfString:@"声明："]];
    // 字体
    [attrString addAttribute:NSFontAttributeName
                       value:FONT(14)
                       range:[statementContent rangeOfString:@"声明："]];
   
    statementLabel.attributedText = attrString;
    
    // 设置尺寸
    statementLabel.height = [statementContent getBoundingRectOfStringSize:CGSizeMake(statementLabel.width, 1000) font:10].size.height + 5;
    statementBgView.height = statementLabel.height + 20;
    
    // 咨询视图
    x = 20;
    y = statementBgView.bottom + 20;
    width = 100;
    height = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.font = FONT(14);
    titleLabel.text = @"咨询内容";
    titleLabel.textColor = SS_NAVI_ORANGE_COLOR;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:titleLabel];
    
    // 咨询输入框
    y = titleLabel.bottom;
    width = SCREEN_WIDTH - 2 * x;
    height = width * 0.7;
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    textView.tintColor = [UIColor orangeColor];
    textView.layer.borderColor = SS_NAVI_ORANGE_COLOR.CGColor;
    textView.layer.borderWidth = 1;
    textView.layer.cornerRadius = 4;
    textView.tag = 111;
    
    [self.view addSubview:textView];
    
    
    // 提交按钮
    y = textView.bottom + 20;
    height = 40;
    UIButton *commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    commitButton.frame = CGRectMake(x, y, width, height);
    commitButton.backgroundColor = SS_NAVI_ORANGE_COLOR;
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    commitButton.layer.cornerRadius = 5;
    [self.view addSubview:commitButton];
}


#pragma mark - 点击事件
- (void)commitAction:(UIButton *)button {
    UserModel *user = [CommonCache getUserInfo];
    if (user == nil) {
        LoginVC *vc = [[LoginVC alloc] init];
        BaseNavVC *navi = [[BaseNavVC alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return;
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:_pid forKey:@"pid"];
    UITextView *textView = (UITextView *)[self.view viewWithTag:111];
    [params setObject:textView.text forKey:@"ask_content"];
    [params setObject:user.uid forKey:@"ask_uid"];
    [params setObject:user.username forKey:@"ask_username"];

    [BoyeHttpClient post:@"BY_Product_ask" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
        
        [SVProgressHUD showSuccessWithStatus:@"发表成功" withDuration:1.5];
        [self performSelector:@selector(leftAction:) withObject:nil afterDelay:1.5];
        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain withDuration:1.5];
    }];
    
}








#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
